//
//  XuanZeKeShiVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/6/3.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "XuanZeKeShiVC.h"
#import "SYRequestAPI+FuDao.h"
#import "ZXFDXuanZeZiYuanList.h"

@interface XuanZeKeShiVC (){
    //记录当前的编辑对象
    int currentIndex;
    NSMutableDictionary *mainParamaDic;
}
//资源选择
@property (nonatomic, strong) ZXFDXuanZeZiYuanList *xuanZeZiYuanList;
@property (nonatomic, strong) ZaiXianFuDaoTianJiaXueShengView *xueShengView;

@end

@implementation XuanZeKeShiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    mainParamaDic = [NSMutableDictionary new];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.tableList];
//    [self addNoticeForKebord];
}
- (void)layout{
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_navView.mas_bottom);
        make.bottom.equalTo(@0);
    }];
}
- (ZXFDCCVNav *)navView{
    if (!_navView) {
        ZXFDCCVNav *view = [[ZXFDCCVNav alloc]init];
        view.backgroundColor = MAINBACKCOLOR;
        view.titleLabe.text = @"添加课时";
        view.delegate = self;
        _navView = view;
    }
    return _navView;
}
- (void)backClick{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(self.view.frame.size.width,0, self.view.frame.size.width, self.view.frame.size.height);
    }completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        tvc.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self addChildViewController:tvc];
        _tableList = tvc.tableView;
        [self.view addSubview:_tableList];
        _tableList.delegate = self;
        _tableList.dataSource = self;
        _tableList.backgroundColor = [UIColor whiteColor];
        [_tableList registerClass:[XuanZeKeShiTopCell class] forCellReuseIdentifier:@"XuanZeKeShiTopCell"];
        [_tableList registerClass:[XuanZeKeShiDefaultOtherView class] forCellReuseIdentifier:@"XuanZeKeShiDefaultOtherView"];
        _tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1+self.fuDaoTimesInt;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == self.fuDaoTimesInt) {
        return 80;
    }
    return 21;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 150;
    }
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        XuanZeKeShiTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XuanZeKeShiTopCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[XuanZeKeShiTopCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XuanZeKeShiTopCell"];
        }
        cell.banJiMingChengView.rightDetailTF.text = self.className;
        cell.fuDaoLaoShiView.rightDetailTF.text = self.teacherName;
        cell.fuDaoLeiXingView.rightDetailTF.text = self.teacheType;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        XuanZeKeShiDefaultOtherView *cell = [tableView dequeueReusableCellWithIdentifier:@"XuanZeKeShiDefaultOtherView" forIndexPath:indexPath];
        if (!cell) {
            cell = [[XuanZeKeShiDefaultOtherView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XuanZeKeShiDefaultOtherView"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.index = (int)indexPath.section;
        cell.fuDaoCiShuView.rightDetailTF.text = [NSString stringWithFormat:@"%d",(int)indexPath.section];
        
        cell.editeMainTitleBlock = ^(NSString * _Nonnull title, int index) {
          //  NSLog(@"编辑主题 %@",title);
            int type = 1;
            [self editeContent:index and:type and:title];
        };
        cell.editeContentClock = ^(NSString * _Nonnull title, int index) {
          //  NSLog(@"编辑内容 %@",title);
            int type = 2;
            [self editeContent:index and:type and:title];
        };
        cell.startBlock = ^(NSString * _Nonnull date, int index) {
          //  NSLog(@"开始时间 %@",date);
            int type = 3;
            [self editeContent:index and:type and:date];
        };
        cell.endBlock = ^(NSString * _Nonnull date, int index) {
          //  NSLog(@"结束时间 %@",date);
            int type = 4;
            [self editeContent:index and:type and:date];
        };
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 21);
    if (section == self.fuDaoTimesInt) {
        [self.commitBtn removeFromSuperview];
        self.commitBtn = nil;
        view.frame = CGRectMake(0, 0, self.view.frame.size.width, 80);
        [view addSubview:self.commitBtn];
    }
    return view;
    
}
- (UIButton *)commitBtn{
    if (!_commitBtn) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.view.frame.size.width/2-75, 10, 130, 40);
        [button setTitle:@"下一步" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds  =YES;
        button.layer.borderColor = SELECTEDCOLOR.CGColor;
        button.layer.borderWidth = 1;
        [button addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _commitBtn = button;
    }
    return _commitBtn;
}
- (void)commitBtnClick:(UIButton *)sender{
    [self combineAllClassTimes:mainParamaDic];
    //创建课时
}
//- (NSMutableArray *)everyTimesArray{
//    if (!_everyTimesArray) {
//        NSMutableArray *array = [NSMutableArray new];
//        _everyTimesArray = array;
//    }
//    return _everyTimesArray;
//}
#pragma mark - 编辑内容
- (void)editeContent:(int)index and:(int)type and:(NSString *)data{
    NSString *mainTitle = @"";
    NSString *mainContent = @"";
    NSString *mainStartTime = @"";
    NSString *mainEndTime = @"";
    NSString *key = @"";
    if (type == 1) {
        //编辑主题
        mainTitle = data;
        key = @"mainTitle";
    }else if (type == 2){
        //编辑内容
        mainContent = data;
        key = @"content";
    }else if (type == 3){
        //开始时间
        mainStartTime = data;
        key = @"beginDate";
    }else if (type == 4){
        
        //结束时间
        mainEndTime = data;
        key = @"endDate";
    }
    if ([mainParamaDic.allKeys containsObject:[NSString stringWithFormat:@"%d",index]]) {
        NSMutableDictionary *dic = [mainParamaDic objectForKey:[NSString stringWithFormat:@"%d",index]];
        [dic setValue:data forKey:key];
        [mainParamaDic setValue:dic forKey: [NSString stringWithFormat:@"%d",index]];
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:data forKey:key];
        [mainParamaDic setValue:dic forKey: [NSString stringWithFormat:@"%d",index]];
    }
}
#pragma mark - 添加逐个课时
- (void)combineAllClassTimes:(NSDictionary *)dic{
    NSMutableArray *parama = [NSMutableArray new];
    NSArray *keys = mainParamaDic.allKeys;
    for (NSString *key in keys) {
        NSMutableDictionary *dic = [mainParamaDic objectForKey:key];
        [dic setValue:key forKey:@"lessonTimes"];
        [dic setValue:@"0" forKey:@"id"];
        [parama addObject:dic];
    }
    [self createKeShiRequest:parama];
}
#pragma mark - 创建课时请求
- (void)createKeShiRequest:(NSMutableArray *)array{
//    self.classID = @"1"; lessonTimes
//    @{lessonTimes:@"1",};
    NSDictionary *parama;
    if (array.count > 0) {
        parama = @{@"classId":self.classID,
                   @"lessons":array
        };
    } else {
        NSMutableArray *dataArr = [NSMutableArray array];
        for (NSInteger i = 1 ; i <= self.fuDaoTimesInt; i ++ ) {
            NSMutableDictionary *dict = [NSMutableDictionary new];
            [dict setValue:[NSString stringWithFormat:@"%ld",i] forKey:@"lessonTimes"];
            [dataArr addObject:dict];
        }
        parama = @{@"classId":self.classID,
                   @"lessons":dataArr
        };
    }
    
   // NSLog(@"上传的创建课时请求参数 %@",parama);
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI fd_TutorTutorTutorTypeClassLessonAdd:parama success:^(id responseObject) {
      [self.view addSubview:self.xueShengView];
       self.xueShengView.fdID = self.classID;
       [self->_xueShengView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(@0);
           make.top.equalTo(@0);
           make.right.equalTo(@0);
           make.bottom.equalTo(@0);
       }];
        [self.view endEditing:YES];
        [MBProgressHUD mj_hideHUDForView:self.view];
        [XSInfoView showInfo:[responseObject objectForKey:@"msg"] onView:self];
    } fail:^(NSError *error) {
        [XSInfoView showInfo:error.message onView:self];
        [MBProgressHUD mj_hideHUDForView:self.view];
       // NSLog(@"创建课时失败 %@",error);
    }];
}

- (ZaiXianFuDaoTianJiaXueShengView *)xueShengView{
    if (!_xueShengView) {
        ZaiXianFuDaoTianJiaXueShengView *view = [[ZaiXianFuDaoTianJiaXueShengView alloc]init];
        _xueShengView = view;
    }
    return _xueShengView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
//#pragma mark - 键盘弹出
//- (void)addNoticeForKebord{
//    //监听事件键盘
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillShow:)
//                                                 name:UIKeyboardWillShowNotification
//                                               object:nil];
//
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillHide:)
//                                                 name:UIKeyboardWillHideNotification
//                                               object:nil];
//}
#pragma mark - keyboard

////当键盘出现或改变时调用
//- (void)keyboardWillShow:(NSNotification *)note {
//    //取出键盘最终的frame
//    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    //取出键盘弹出需要花费的时间
//    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
////    [self.tableList mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.top.equalTo(@44).offset(-(index-1)*300);
////    }];
//}
//
////当键盘消失时调用
//- (void)keyboardWillHide:(NSNotification *)note {
//    //取出键盘弹出需要花费的时间
//    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
////    [self.tableList mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.top.equalTo(@44).offset(0);
////    }];
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
